package com.wcs.exam.controller.api;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.captcha.generator.RandomGenerator;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.wcs.exam.common.base.Result;
import com.wcs.exam.common.cache.CacheRedis;
import com.wcs.exam.common.constant.RedisConstant;
import com.wcs.exam.common.util.EnumUtil;
import com.wcs.exam.controller.api.resp.ApiVerCodeResp;
import com.wcs.exam.controller.api.resp.ApiWebsiteResp;
import com.wcs.exam.controller.biz.SysConfigCommonBiz;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author wcs
 */
@Slf4j
@Tag(name = "Api-通用接口")
@CacheConfig(cacheNames = RedisConstant.CONFIG_CACHE_NAME)
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/v1/api/common")
public class ApiCommonController {

    @NotNull
    private final CacheRedis cacheRedis;
    @NotNull
    private final SysConfigCommonBiz sysConfigCommonBiz;

    @Operation(summary = "获取枚举信息")
    @GetMapping(value = "/enum")
    public Result<List<?>> getEnumInfo(@RequestParam String enumName) {
        try {
            Class<?> clazz = Class.forName(getClassName(enumName));
            return Result.success(new ArrayList<>(Objects.requireNonNull(EnumUtil.toList(clazz))));
        } catch (ClassNotFoundException e) {
            log.error("获取枚举失败, enumName={}", enumName, e);
            return Result.error("获取枚举失败");
        }
    }

    @Cacheable
    @Operation(summary = "获取网站基本信息")
    @GetMapping(value = "/website")
    public Result<ApiWebsiteResp> website() {
        ApiWebsiteResp resp = sysConfigCommonBiz.getSysConfig(ApiWebsiteResp.class);
        // 公安网备案号处理
        resp.setWebsitePrnNo(getNumeric(resp.getWebsitePrn()));
        return Result.success(resp);
    }

    @Operation(summary = "获取图片验证码")
    @GetMapping(value = "/code")
    public Result<ApiVerCodeResp> getVerCode() {
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, new RandomGenerator(RandomUtil.BASE_NUMBER, 4), 12);
        ApiVerCodeResp resp = new ApiVerCodeResp(String.valueOf(IdWorker.getId()), captcha.getImageBase64Data());
        // 存入缓存，过期时间为5分钟
        cacheRedis.set(RedisConstant.System.VER_CODE + resp.getVerToken(), captcha.getCode().toLowerCase(), 5, TimeUnit.MINUTES);
        return Result.success(resp);
    }

    // 获取枚举类的全路径
    private static String getClassName(String enumName) {
        // 首字母大写
        enumName = enumName.substring(0, 1).toUpperCase() + enumName.substring(1);
        return EnumUtil.DEFAULT_PREFIX + enumName + "Enum";
    }

    private static String getNumeric(String str) {
        String regEx = "[^0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }

}
